using System;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseForum.Mockup;
using BaseForum.Model;
using BaseForum.Dao;

namespace WebForum.App_Data
{
    public class TopicDataSource
    {
        private static readonly TopicDao topicDao = new TopicDao();
        /// <summary>
        /// Get all Parent Topic in Sub Forum
        /// </summary>
        /// <param name="fID"></param>
        /// <returns></returns>
        public static ArrayList List(int fID)
        {
            ArrayList list = new ArrayList();
            foreach (Topic t in topicDao.List(ForumDataSource.Get(fID)))
            {
                t.User = UserDataSource.Get(t.User.Id);
                list.Add(t);
            }
            return list;
        }
        /// <summary>
        /// Get topic by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Topic Get(int id)
        {
            return topicDao.GetFullWithForum(id);
        }
        /// <summary>
        /// Get full info of topic
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Topic GetFull(int id)
        {
            return topicDao.GetFullWithForum(id);
        }
        /// <summary>
        /// List Sub topic
        /// </summary>
        /// <param name="pID"></param>
        /// <returns></returns>
        public static ArrayList ListSubTopics(int pID)
        {
            ArrayList list = new ArrayList();
            foreach (Topic t in topicDao.GetFull(pID).ChildTopics)
            {
                t.User = UserDataSource.Get(t.User.Id);
                list.Add(t);
            }
            return list;
        }
        /// <summary>
        /// Insert topic
        /// </summary>
        /// <param name="topic"></param>
        /// <returns></returns>
        public static bool Insert(Topic topic)
        {
            return topicDao.Insert(topic);
        }
        /// <summary>
        /// Update
        /// </summary>
        /// <param name="topic"></param>
        /// <returns></returns>
        public static bool Update(Topic topic)
        {
            return topicDao.Update(topic);
        }
        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Delete(Int32 id)
        {
            return topicDao.Delete(id);
        }
        public static bool IsOwner(User u, Topic t)
        {
            return topicDao.IsOwner(u, t);
        }
    }
}
